<?php
/**
 * 标签资讯控件(根据标签获取资讯)
 * taginfocontroller.php
 * Create on 2015-07-09
 * Create by yuebin
 */
namespace haibao\cms\business\control\wap\common;
use haibao\frame\data\query\Condition;

class TagInfoController extends \haibao\cms\business\control\Base{

	const DAILY_BITCH_TAG_ID = 11092;
	const TALK_SHOW_TAG_ID = 11171;
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){
		
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$path = $this->generateHtmlPath(__CLASS__);
		
		if($controlData['manual']){
			
			// 根据频道ID获取频道编码
			$siteChannelInfo = \haibao\cms\business\site\SiteChannel::getSiteChannelById($this->getParameter(self::$CHANNEL_ID));
			if($siteChannelInfo&&$siteChannelInfo->ChannelCode){
				if($siteChannelInfo->ChannelCode == \haibao\cms\business\site\SiteChannel::DAILY_BITCH_LIST ||
					$siteChannelInfo->ChannelCode == \haibao\cms\business\site\SiteChannel::DAILY_BITCH_INDEX
				){
					$this->conditionTagId = self::DAILY_BITCH_TAG_ID;
				}elseif($siteChannelInfo->ChannelCode == \haibao\cms\business\site\SiteChannel::TALK_SHOW_LIST || 
					$siteChannelInfo->ChannelCode == \haibao\cms\business\site\SiteChannel::TALK_SHOW_INDEX
				){
					$this->conditionTagId = self::TALK_SHOW_TAG_ID;
				}
				$dataCondition = $controlData['manual'];
				$dataCondition['currentPage'] = $this->getParameter(self::$CURRENTPAGE);
				$this->getTagInfoList($dataCondition);
			}
		}
		$this->assign('currentPage',$this->getParameter(self::$CURRENTPAGE));
		$this->generateHtml($path,$content);
		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content);
		return $content;
	}
	
	/**
	 * 获取资讯列表
	 */
	private function getTagInfoList($dataCondition){
		$dataCondition['level'] = 1;
		$dataCondition['isTag'] = true;
		$dataCondition['isParentTag'] = false;
		$dataCondition['isProImage'] = true;
		$dataCondition['isImageColl'] = false;
		$dataCondition['isReturnTag'] = false;
		$dataCondition['isPage'] = true;
		$dataCondition['proImageLevel'] = \haibao\cms\model\data\ArticleProImage::Level_FOUR;
		$dataCondition['source'] = __CLASS__.$this->conditionTagId;
		$this->conditionTagId = (array)$this->conditionTagId;
		list($articles,$articleProImage) = $this->getArticleControl(1,$dataCondition);
		$infoList = $articleIdArr = $wapInfoArr = array();
		if($articles){
			foreach($articles as $key=>$info){
				$info = $info->toArray();
				$info['PublishTime'] = $this->formatDate($info['PublishTime']->format('Y-m-d H:i:s'));
				$info['Channel'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/articlem/'.$info['PublishId'].'.htm';
				$info['ProImage'] =  isset($articleProImage[$info['Id']][\haibao\cms\model\data\ArticleProImage::Level_FOUR])?$articleProImage[$info['Id']][\haibao\cms\model\data\ArticleProImage::Level_FOUR]:
				(isset($articleProImage[$info['Id']][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$articleProImage[$info['Id']][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
			
				array_push($infoList,$info);
				array_push($articleIdArr,$info['Id']);
			}
			
			$wapInfoFilter = new \haibao\cms\model\filter\WapInfo();
			$wapInfoFilter->select(
					array(
							\haibao\cms\model\filter\WapInfo::CONDITION_FIELD_NAME_TITLE,
							\haibao\cms\model\filter\WapInfo::CONDITION_FIELD_NAME_INFO_ID
			));
			$wapInfoFilter->where(\haibao\cms\model\filter\WapInfo::CONDITION_FIELD_NAME_INFO_ID,Condition::CONDITION_IN,$articleIdArr);
			$wapInfoFilter->where(\haibao\cms\model\filter\WapInfo::CONDITION_FIELD_NAME_INFO_TYPE,Condition::CONDITION_EQUAL,\haibao\cms\model\data\WapInfo::INFO_TYPE_ARTICLE);
			$wapInfoList = \haibao\cms\business\WapInfo::getInfoList($wapInfoFilter);
			foreach($wapInfoList as $list){
				$wapInfoArr[$list->InfoId] = $list->toArray();
			}
			
			foreach($infoList as $key=>$list){
				if(isset($wapInfoArr[$list['Id']])) $infoList[$key]['Title'] = $wapInfoArr[$list['Id']]['Title'];
			}
		}
		$this->assign('infoList',$infoList);
	}
	
}